package textBuddyProcessor;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/*
 * ==============NOTE TO USER==========================================
 * This class checks the input, for incorrect or no arguments
 * asks the user to continue accordingly
 * ====================================================================
 */

public class InputValidator {
	private static final String MESSAGE_ASK_TO_CONTINUE = "Do you wish to continue,"
			+ "Press C/c to continue or 'exit' to terminate";
	private static final String MESSAGE_ASK_FILE_NAME = "Please enter the name of "
			+ "the file you wish to use";
	private static final String MESSGAE_INVALID = "Invalid File name";
	private static final String MESSGAE_NO_ARGS = "No Arguments given";
	private static final String MESSAGE_EXIT = "exit";

	private static final int EXIT_INT = 0;
	private static final int EMPTY = 0;
	private static final String MESSAGE_CONTINUE = "c";

	public boolean checkIfWrongFileName(String nameOfFile, Scanner scannerToRead) {
		File temporaryFile = new File(nameOfFile);
		try {
			// checks if correct file name according to standards
			temporaryFile.getCanonicalPath();
			return false;
		} catch (IOException e) {
			stopWithErrorMessageOrProceed(MESSGAE_INVALID, scannerToRead);
		}
		return true;

	}

	public boolean checkIfNoArguments(String[] userCommand,
			Scanner scannerToRead) {
		if (userCommand.length == EMPTY) {
			stopWithErrorMessageOrProceed(MESSGAE_NO_ARGS, scannerToRead);
			return true;
		}
		return false;

	}

	private void stopWithErrorMessageOrProceed(String errorMessage,
			Scanner scannerToRead) {
		System.out.println(errorMessage);
		askUserToProceed(scannerToRead);
	}

	private void askUserToProceed(Scanner scannerToRead) {
		System.out.println(MESSAGE_ASK_TO_CONTINUE);
		String ans = scannerToRead.next();
		if (ans.equals(MESSAGE_EXIT)) {
			System.exit(EXIT_INT);
		} else if (ans.equalsIgnoreCase(MESSAGE_CONTINUE)) {
			ans = scannerToRead.nextLine();
			checkIfWrongFileName(ans, scannerToRead);
		}

	}

	public String askToReenterData(Scanner scannerToRead) {
		System.out.println(MESSAGE_ASK_FILE_NAME);
		return scannerToRead.nextLine();

	}
}
